//
//  ListenSearchViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/6/25.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ListenSearchViewController.h"
#import "ScanView.h"
#import "HomeRequest.h"
#import "BookListCollectionViewCell.h"
#import "BookDetailViewController.h"

@interface ListenSearchViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic , strong)ScanView *scanView;
@property (nonatomic , strong)NSString *scanKey;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation ListenSearchViewController
//点击屏幕空白处 －－－ 回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(ScanView *)scanView {
    if (!_scanView) {
        _scanView = [[ScanView alloc] initWithFrame:CGRectMake(0, lTopBarHeight, SCREEN_W, 10+40)];
        WS(WeakSelf)
        _scanView.searchBlock = ^{
            WeakSelf.page = 1;
            [WeakSelf loadData];
        };
        _scanView.searchTextBlock = ^(NSString *text) {
            WeakSelf.scanKey = text;
        };
    }
    return _scanView;
}
- (void)loadData {
    NSString *type = @"听说";
    [HomeRequest getTextbookListWithKeyWord:self.scanKey type:type page:self.page result:^(NSMutableArray<TextbookModel *> *list) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (self.page == 1) {
            [self.dataArr removeAllObjects];
        }
        [self.dataArr addObjectsFromArray:list];
        [self.collectionView reloadData];
                if (self.dataArr.count) {
                    self.collectionView.mj_footer.hidden = NO;
                }
    }];
}
- (void)getData {
    WS(WeakSelf)
    [HomeRequest scanBookWithKey:self.scanKey page:self.page result:^(NSMutableArray<ScanBookModel *> *list) {
        NSLog(@"=== %@ ==",list);
        [WeakSelf.collectionView.mj_header endRefreshing];
        [WeakSelf.collectionView.mj_footer endRefreshing];
        if (WeakSelf.page == 1) {
            [WeakSelf.dataArr removeAllObjects];
        }
        [WeakSelf.dataArr addObjectsFromArray:list];

        [WeakSelf.collectionView reloadData];
    }];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"搜索";
    self.page = 1;
    
    [self setUI];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];
    self.collectionView.mj_header = header;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self loadData];
    }];
    self.collectionView.mj_footer = footer;
    self.collectionView.mj_footer.hidden = YES;
}

- (void)setUI {
    [self.view addSubview:self.scanView];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10; // 水平间距
    layout.minimumLineSpacing = 15; // 垂直间距
    layout.sectionInset = UIEdgeInsetsMake(15, 10, 15, 10); // 边界间距
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, lTopBarHeight + 60, SCREEN_W, SCREEN_H - SafeAreaBottomHeight - lTopBarHeight - 60) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.alwaysBounceVertical = YES;
    collectionView.pagingEnabled = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    [collectionView registerNib:[UINib nibWithNibName:@"BookListCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"BookListCollectionViewCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BookListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BookListCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"BookListCollectionViewCell" owner:self options:nil] lastObject];
    }
    cell.scanBook = self.dataArr[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_W - 40)/3 - 1, 192);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TextbookModel *model = self.dataArr[indexPath.row];
    BookDetailViewController *vc = [[BookDetailViewController alloc] init];
    vc.productId = model.productId;
    vc.type = BookTypeHearsay;
    [self.navigationController pushViewController:vc animated:YES];
}


- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}


@end
